<?php
    namespace Home\Controller;
    use Think\Controller;
    class KpizhibiaoshezhiController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");

        $data = M("hospitalsys.guide_gather_class")->limit(($page-1)*$limit,$limit)->select();
        $type_list_2_3 = M("hospitalsys.jxgl_guide_bsc_class_dict")->select();
        
        $type_list_1 = M("hospitalsys.jxgl_guide_organ_class_dict")->select();

        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];
            
            $type_id_2 = $info['guide_attr'];
            $type_id_1 = $info['organ_class'];

            for ($j=0; $j < count($type_list_1); $j++) 
            { 
                if($type_list_1[$j]["organ_class_code"] == $type_id_1)
                {
                    $data[$i]['organ_class_name'] = $type_list_1[$j]['organ_class_name'];
                }
            }

            for ($j=0; $j < count($type_list_2_3); $j++) 
            { 
                if($type_list_2_3[$j]["bsc_class_code"] == $type_id_2)
                {
                    $data[$i]['bsc_class_name'] = $type_list_2_3[$j]['bsc_class_name'];
                }
            }

        }
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("hospitalsys.guide_gather_class")->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $guide_gather_code = I("post.guide_gather_code");

        $info = M("hospitalsys.guide_gather_class")->where("guide_gather_code = '$guide_gather_code'")->find();
        $info["dept_date"] = date("Y-m-d H:i:s",$info["dept_date"]);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $guide_gather_code = $data["guide_gather_code"];
        

        $result = M("hospitalsys.guide_gather_class")->where("guide_gather_code = '$guide_gather_code'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        
        $result = M("hospitalsys.guide_gather_class")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function get_cbhs_cost_item_dict()
    {
        $list = M("cbhs.cbhs_cost_item_dict")->select();
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }

    public function get_jxgl_guide_bsc_class_dict_list()
    {
        $type = I('post.type');
        $code = I('post.code');

        $list = M("hospitalsys.jxgl_guide_bsc_class_dict")->select();
        $arr = [];
        for ($i=0; $i < count($list); $i++) 
        {
            
            if($type == 1)
            {
                if(strlen($list[$i]['bsc_class_code']) == 2)
                {
                    $arr[] = $list[$i];
                }
            }
            

            if($type == 2)
            {
                if(strlen($list[$i]['bsc_class_code']) == 4)
                {
                    if(!empty($code))
                    {
                        if(substr($list[$i]['bsc_class_code'], 0,2) == $code)
                        {
                            $arr[] = $list[$i];
                        }
                    }
                    else
                    {
                        $arr[] = $list[$i];
                    }
                    
                }
            }
        }

        $json["data"]   = $arr;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }

    public function get_jxgl_guide_organ_class_dict_list()
    {
        $list = M("hospitalsys.jxgl_guide_organ_class_dict")->select();
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }

    public function get_guide_name_dict()
    {
        // $page = I("get.page");
        // $limit = I("get.limit");

        $guide_gather_code = I('get.guide_gather_code');

        $list = M("hospitalsys.guide_name_dict")->select();
        
        $guide_gathers_list = M("hospitalsys.guide_gathers")->where("guide_gather_code = '$guide_gather_code'")->select();
        
        $type_list_2_3 = M("hospitalsys.jxgl_guide_bsc_class_dict")->select();
        for ($i=0; $i < count($list); $i++) 
        {
            $bsc = $list[$i]['bsc'];
            for ($j=0; $j < count($type_list_2_3); $j++) 
            {
                if($type_list_2_3[$j]["bsc_class_code"] == $bsc)
                {
                    $list[$i]['bsc_class_name'] = $type_list_2_3[$j]['bsc_class_name'];
                }
            }

            for ($j=0; $j < count($guide_gathers_list); $j++) 
            { 
                if($guide_gathers_list[$j]["guide_code"] == $list[$i]["guide_code"])
                {
                    $list[$i]['LAY_CHECKED'] = true;
                }
            }

        }

        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }

    public function add_guide_gathers()
    {
        $guide_gather_code = I('post.guide_gather_code');
        $guide_code_list = I('post.guide_code_list');

        $data['guide_gather_code'] = $guide_gather_code;

        $guide_code_list = explode(",",$guide_code_list);

        M('hospitalsys.guide_gathers')->where("guide_gather_code = '$guide_gather_code'")->delete();
        
        for ($i=0; $i < count($guide_code_list); $i++)
        { 
            $data['guide_code'] = $guide_code_list[$i];
            
            
            $result =  M("hospitalsys.guide_gathers")->add($data);
            
        }
        $json["data"]   = "";
        $json["code"]   = 0;
        $json["msg"]    = "添加成功";
        $this->ajaxReturn($json,"json");
    }

    public function select_keshi()
    {
        //DEPT_GATHERS  dept_code
        $page = I("get.page");
        $limit = I("get.limit");
        //SYS_DEPT_INFO dept_code
        $account_date = I("get.account_date");

        if(empty($account_date))
        {
            die();
        }

        $account_date = strtotime($account_date);

        $sql = "SELECT X.DEPT_NAME,
           x.dept_code,
           x.account_date,
           y.GATHER_CODE,
           y.STATION_BSC_CLASS_01,
           y.STATION_BSC_CLASS_02,
           y.STATION_BSC_CLASS_03,
           y.STATION_BSC_CLASS_04
      FROM  (select d.dept_code ,d.account_date,d.dept_name from COMM.SYS_DEPT_INFO d where account_date = $account_date and attr =1) x
           LEFT JOIN
           (SELECT a.DEPT_CODE ,
                   a.GATHER_CODE,
                   a.STATION_BSC_CLASS_01,
                   a.STATION_BSC_CLASS_02,
                   a.STATION_BSC_CLASS_03,
                   a.STATION_BSC_CLASS_04,
                   B.GUIDE_GATHER_NAME
              FROM HOSPITALSYS.DEPT_GATHERS a, HOSPITALSYS.GUIDE_GATHER_CLASS b
             WHERE A.GATHER_CODE = B.GUIDE_GATHER_CODE) y
              ON x.DEPT_CODE = y.DEPT_CODE and rownum < 10 order by x.dept_code";



        $data = M()->query($sql);

        $list = M("hospitalsys.guide_gather_class")->select();

        for ($i=0; $i < count($data); $i++) 
        { 
            $data[$i]['list'] = $list;
        }

        $arr = [];

        for ($i= ($page-1)*$limit; $i < ($page*$limit); $i++) 
        { 
            $arr[] = $data[$i];
        }

        $json["data"] = $arr;
        $json["code"] = 0;
        $json["count"] = count($data);
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");

    }

    public function set_field()
    {
        $table  = I("post.table");
        $col    = I("post.col");
        $pk     = I("post.pk");
        $value  = I("post.value");

        $data[$col] = $value;

        $info = M($table)->where($pk)->find();
        if(empty($info))
        {
            $pk[$col] = $value;
            $result = M($table)->add($pk);
        }
        else
        {
            $result = M($table)->where($pk)->save($data);
        }
        if($result !== false)
        {
            echo 1;
        }
        else
        {
            echo 2;
        }
    }

    public function get_guide_gather_class()
    {
        
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = 0;
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");

    }

    public function edit_keshi()
    {
        $dept_code = I('get.dept_code');
        $station_year = I('get.account_date');

        $sql = "SELECT X.GUIDE_CODE,
         X.GUIDE_NAME,
         X.DEPT_CODE,
         Y.GUIDE_VALUE,
         Y.GUIDE_CAUSE,
         Y.GUIDE_UNIT,
         Y.INCREASE,
         Y.INCREASE_ARITHMETIC,
         Y.DECREASE,
         Y.DECREASE_ARITHMETIC,
         Y.STATION_YEAR,
         Y.MINUSFLAG,
         Y.PLUSFLAG,
         Y.MINUS_LIMIT,
         Y.PLUS_LIMIT
    FROM (SELECT C.DEPT_CODE,
                 A.GUIDE_GATHER_CODE,
                 A.GUIDE_CODE,
                 B.GUIDE_NAME,
                 C.STATION_YEAR
            FROM HOSPITALSYS.GUIDE_GATHERS A,
                 HOSPITALSYS.DEPT_GATHERS C,
                 HOSPITALSYS.GUIDE_NAME_DICT B
           WHERE     A.GUIDE_GATHER_CODE = C.GATHER_CODE
                 AND A.GUIDE_CODE = B.GUIDE_CODE
                 AND C.STATION_YEAR = '$station_year'
                 AND C.DEPT_CODE = '$dept_code') X
         LEFT JOIN (SELECT DEPT_CODE,
                           GUIDE_CODE,
                           GUIDE_VALUE,
                           GUIDE_CAUSE,
                           GUIDE_UNIT,
                           INCREASE,
                           INCREASE_ARITHMETIC,
                           DECREASE,
                           DECREASE_ARITHMETIC,
                           STATION_YEAR,
                           MINUSFLAG,
                           PLUSFLAG,
                           MINUS_LIMIT,
                           PLUS_LIMIT
                      FROM HOSPITALSYS.DEPT_GUIDE_INFORMATION D
                     WHERE D.STATION_YEAR = '$station_year' AND D.DEPT_CODE = '$dept_code') Y
            ON X.GUIDE_CODE = Y.GUIDE_CODE
ORDER BY X.GUIDE_CODE ASC";


        $data = M()->query($sql);


        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = 0;
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }


    
}